package com.itheima.knowledge.manager.controller;

import com.itheima.knowledge.manager.domain.Catalog;
import com.itheima.knowledge.manager.domain.Knowledge;
import com.itheima.knowledge.manager.service.KnowledgeService;

import java.security.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

import static com.itheima.knowledge.manager.entry.KnowledgeEntry.kd;

public class KnowledgeController {
    private String flag;

    public KnowledgeController() {
    }

    public KnowledgeController(String flag) {
        this.flag = flag;
    }

    Scanner sc = new Scanner(System.in);
    KnowledgeService ks = new KnowledgeService();
    public void deleteCatalog() {
        System.out.println("请输入您要删除的知识选项:");
        String id;
        while (true) {
            id = sc.nextLine();
            if (id.equals("c")||id.equals("u")||id.equals("d")||id.equals("e")){
                System.out.println("无权删除系统选项!");
                return;
            }
            boolean exists = ks.existsCatalog(id);
            if (exists){
                ks.deleteCatalog(id);
                System.out.println("删除成功!");
                break;
            }else{
                System.out.println("您输入的选项不存在");
                break;
            }
        }

    }

    public void updataCatalog() {
        System.out.println("请输入您要修改的知识选项:");
        String id;
        while (true) {
            id = sc.nextLine();
            boolean exists = ks.existsCatalog(id);
            if (exists){
                break;
            }else{
                System.out.println("您输入的选项不存在,请重新输入:");
            }
        }
        System.out.println("请输入您要更新后的知识点:");
        String knowledge = sc.nextLine();
        ks.updataCatalog(id,knowledge);
        System.out.println("修改成功!");
    }

    public void addCatalog() {
        String id;
        System.out.println("请输入您要添加的知识选项:");
        while (true) {
            id = sc.nextLine();
            boolean exists = ks.existsCatalog(id);
            if (!exists) {
                break;
            } else {
                System.out.println("你输入的知识选项已经存在,请重新输入:");
            }
        }
        System.out.println("请输入您要添加的知识点:");
        String know = sc.nextLine();
        Catalog catalog = new Catalog(id,know);
        ks.addCatalog(catalog);
        System.out.println("添加成功!");
    }

    public void start(String know) {
        while (true) {
        ArrayList<Knowledge> arrays = ks.findAllLogid(know);
        System.out.println("--------欢迎来到 <"+know+"> 知识管理系统--------");
        //这里是才是不需要,不对功能不一样,功能浏览全局,下面是浏览知识
        System.out.println("编号\t\t题目\t\t\t\t添加时间");
        for (int i = 0; i < arrays.size(); i++) {
            Knowledge kn = arrays.get(i);
            if (kn != null) {
                System.out.println(kn.getKnid()+ "\t\t" + kn.getSketch() +"\t\t" +kn.getTime());
            }
        }
            System.out.println("请输入您的选择: 1.添加知识点  2.删除知识点  3.修改知识点  4.查看知识点  5.退出");
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    //添加
                    addKnowledge(know);
                    break;
                case "2":
                    //删除
                    deleteKnowledge(know);
                    break;
                case "3":
                    //修改
                   updataKnowledge(know);
                    break;
                case "4":
                    //查看
                    findAllKnowlege(know);
                    break;
                case "5":
                    //退出
                    System.out.println("感谢使用"+know+"知识管理系统,再见!");
                    return;
                default:
                    System.out.println("您输入有误,请重新输入");
                    break;

            }
        }
    }

    private void updataKnowledge(String know) {
        System.out.println("请输入需要修改的知识编号:");
        String id = sc.nextLine();
        String textpart =null;
        String remark = null;
        int cho;
        if (ks.existsKnid(id,know)){
            System.out.println("请输入您要修改的标题:");
            String sketch = sc.nextLine();
           loop: while (true) {
                System.out.println("修改正文请输入:1,修改备注输入:2");
                cho = sc.nextInt();
                switch (cho){
                    case 1:
                        System.out.println("请输入您要修改的正文:");
                        textpart = ScannerSelf();
                        break loop;
                    case 2:
                        System.out.println("请输入您要修改的备注:");
                        remark = ScannerSelf();
                        break loop;
                        default:
                            System.out.println("输入有误,请重新输入");
                            break;

                }
            }


            // String time = null;
            /*ArrayList<Knowledge> array = kd.findAllLogid(know);
            for (int i = 0; i < array.size(); i++) {
                Knowledge knowledge1 = array.get(i);
                if (knowledge1 != null && knowledge1.getKnid().equals(id)) {
                    time = knowledge1.getTime();
                }
            }*/
            Date date =new Date();
            SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
            String time = simpleDateFormat.format(date);

            Knowledge knowledge = new Knowledge(id,sketch,textpart,remark,time);
            ks.updataKnowledge(id,knowledge,know,cho);
            System.out.println("修改成功!");
        }else {
            System.out.println("输入的编号有误!");
        }
    }

    public void deleteKnowledge(String know) {
        System.out.println("请输入需要删除的知识编号:");
        String id = sc.nextLine();
        ks.deleteKnowledge(id,know);

    }

    //需要进入精确查找
    public void findAllKnowlege(String know) {
        ArrayList<Knowledge> arrays = ks.findAllLogid(know);
        System.out.println("请输入需要查看的知识编号:");
        String  id = sc.nextLine();
        boolean flag = false;
        for (int i = 0; i < arrays.size(); i++) {
            Knowledge kn = arrays.get(i);
            if (kn != null&&kn.getKnid().equals(id)) {
                //System.out.println(kn.getKnid()+ "\n" + kn.getSketch() +"\n"+kn.getTextpart()+"\n" + kn.getRemark()+"\n"+kn.getTime());
                System.out.print("编号为:");
                System.out.println(kn.getKnid());
                System.out.print("标题:");
                System.out.println(kn.getSketch());
                System.out.print("正文:");
                System.out.println(kn.getTextpart());
                System.out.print("备注:");
                System.out.println(kn.getRemark());
                System.out.print("建立时间:");
                System.out.println(kn.getTime());
                flag = true;
            }
        }
        if (!flag){
            System.out.println("输入的编号有误!");
        }
        System.out.println("e.退出");
        while (true) {
            String var = sc.nextLine();
            if (var.equals("e")){
                return;
            }else {
                System.out.println("请输入选择:");
            }
        }
    }

    public void addKnowledge(String know) {
        String id;
        System.out.println("请输入您要添加的"+know+"知识编号:(格式为:"+know+"_00x)");
        while (true) {
            id = sc.nextLine();
            boolean exists = ks.existsKnid(id,know);
            if (!exists) {
                break;
            } else {
                System.out.println("你输入的"+know+"知识编号已经存在,请重新输入:");
            }
        }
        System.out.println("请输入该知识点的题目:");
        String sketch = sc.nextLine();
        System.out.println("请输入具体描述:");
        String textpart = ScannerSelf();
        System.out.println("请输入备注:");
        String remark = ScannerSelf();
        Date date =new Date();
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        String time = simpleDateFormat.format(date);
        //System.out.println(time);
        Knowledge knd = new Knowledge(id,sketch,textpart,remark,time);
        ks.addKnowledge(knd,know);
        System.out.println("添加成功!");
    }
    public String ScannerSelf(){
        String flag = "";
        String sss = "";
        String ss = "";
        do{
            sss=sss+ss+'\n';
            ss = sc.nextLine();
        }while (!ss.equals(":w"));
        return sss;
    }
    }



